<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller as BaseController;
use App\Repository\Product\ProductRepository;
use Illuminate\Http\Request;

// 分销商
class ProductController extends BaseController
{
    private $repoist;

    public function __construct(ProductRepository $model)
    {
        $this->repoist = $model;
    }

    public function store(Request $request)
    {
        return $this->repoist->add($request);
    }

    public function index(Request $request)
    {
        $where = [];
        if (!empty($request->get('phone'))) {
            $where['where']['product_code'] = $request->get('phone');
        }
        if (!empty($request->get('name'))) {
            $where['like'] = ['name' => $request->get('name')];
        }
        if (!empty($request->get('number'))) {
            $where['like'] = ['name' => $request->get('number')];
        }
        $where['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
        $where['limit']['number'] = $request->get('pageSize');
        if ($request->user->role_id != 1) {
            $where['where']['shop_id'] = $request->user->shop_id;
        }
        $data = $this->repoist->index($where);
        $data['isProductTypeList'] = config('card.isProductType');
        return $data;
    }

    public function save(Request $request)
    {
        if (!$request->get('id')) {
            return $this->returnMsg(1, '请传递id参数');
        }
        $where = ['id' => $request->get('id')];
        return $this->repoist->edit($where, $request);
    }

    public function show($id)
    {
        $where = ['id' => $id];
        //查询一条数据
        $return = $this->repoist->getOne($where);
        if (!empty($return)) {
            // $return = usfirstArr($return);
        }
        return $this->returnMsg(200, '查询成功', $return);
    }

    public function listss(Request $request)
    {
        $where = [];
        if (!empty($request->get('name'))) {
            $where['where'] = ['name' => $request->get('name')];
        }

        $return = $this->repoist->lists($where);
        return $return;
    }

    public function destroy(Request $request, $id)
    {
        if (!$id) {
            return $this->returnMsg(1, '请传递参数');
        }
        $where = [
            'id' => $id,
            'shop_id' => $request->user->shop_id,
        ];
        return $this->repoist->del($where);
    }

    public function getAllData(Request $request)
    {
        $where['where'][] = ['status', '=', 0];
        $where['select'] = ['name', 'id', 'status'];
        if ($request->user->role_id != 1) {
            $where['where']['shop_id'] = $request->user->shop_id;
        }
        return $this->repoist->index($where);
    }

    public function getAllDataWhere(Request $request)
    {
        $where = [
            'shop_id' => $request->user->shop_id,
        ];
        $datas = $this->repoist->getAllWhereData($where, ['id', 'name']);
        return $this->returnMsg(200, '查询成功', $datas);
    }

    public function productOptionsList(Request $request)
    {
        $where = [
            'status' => 0,
            'shop_id' => $request->user->shop_id,
        ];
        $datas = $this->repoist->getAllWhereData($where, ['id', 'name']);
        return $this->returnMsg(200, '查询成功', $datas);
    }

    public function updateStatus(Request $request, $id)
    {
        $where = [
            'id' => $id,
            'shop_id' => $request->user->shop_id
        ];
        return $this->repoist->edit($where, $request);
    }
}
